package com.example.controller;

import com.example.dto.LoginResponse;
import com.example.dto.ResponseDTO;
import com.example.service.UserService;
import com.example.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api/v1/auth")
public class AuthController {

    private final JwtTokenUtil jwtTokenUtil;

    @Autowired
    private UserService userService;

    public AuthController(JwtTokenUtil jwtTokenUtil) {
        this.jwtTokenUtil = jwtTokenUtil;
    }

    /**
     * 用户登录接口
     */
    @PostMapping("/login")
    public LoginResponse login(@RequestParam String username, @RequestParam String password) {
        // 通过 UserService 认证用户
        String token = userService.authenticate(username, password);

        // 返回响应，包含 access_token 和 token_type
        return new LoginResponse(token, "bearer");
    }

    /**
     * 获取路由信息接口
     */
    @GetMapping("/routes")
    public ResponseDTO<List<Map<String, Object>>> getRoutes() {
        // 模拟路由数据
        List<Map<String, Object>> routes = new ArrayList<>();

        Map<String, Object> loginRoute = new HashMap<>();
        loginRoute.put("path", "/login");
        loginRoute.put("name", "ROUTER_CONSTANTS.LOGIN");
        loginRoute.put("component", "LoginPage");
        loginRoute.put("meta", Map.of("title", "登录页"));
        routes.add(loginRoute);

        Map<String, Object> notFoundRoute = new HashMap<>();
        notFoundRoute.put("path", "/404");
        notFoundRoute.put("name", "ROUTER_CONSTANTS.NOTFOUND");
        notFoundRoute.put("component", "Layout");
        notFoundRoute.put("meta", Map.of("title", "404"));
        routes.add(notFoundRoute);

        return new ResponseDTO<>(200, routes, "成功获取路由信息");
    }
}
